const logger = require('../logger')
const mongodb = require('../mongodb')
const response = require('../utils/response')
const paginate = require('../utils/paginate')
const captcha = require('trek-captcha')
const {sign} = require('../utils/auth')

class gui{
	
	
		
		constructor(){
			
		 }
		
	// addCode
			// O0WsdJpAE Start
		
         findOneUserInfo = async (ctx)=>{ // 获取用户信息
            
        	  
            
          let errors = await ctx.validationErrors()
          if(errors){
            response.error(ctx,errors[0].msg)
            return
          }
            
        	let where = {}
            
            console.log(ctx.userInfo)
        if(ctx.userInfo&&ctx.userInfo._id){
          where["_id"] = ctx.userInfo._id
        }else{
          response.error(ctx,'请登录')
          return
        }
        
            
            
            
          let result = await mongodb.findOne('user',where)
              
          if(result != false){
            response.success(ctx,result)
          }else{
            response.error(ctx,'服务器错误')
          }
            
            
        }
		// O0WsdJpAE end
			
			// AiV-mKZ4g Start
		
         findOneUserLogin = async (ctx)=>{ // 登录
            
        	  
        	let userName = ctx.request.body.userName
        	let password = ctx.request.body.password
        	let imageCode = ctx.request.body.imageCode
            
        	ctx.checkBody('userName','用户名 不能为空').notEmpty()
        	ctx.checkBody('password','密码 不能为空').notEmpty()
          let errors = await ctx.validationErrors()
          if(errors){
            response.error(ctx,errors[0].msg)
            return
          }
            
        	let where = {userName,password}
            
            if(imageCode != ctx.session.imageCode){ // 后续再严谨一点添加验证码过期时间
          response.error(ctx,'验证码错误，请重新输入')
          return
        }
            
            
            
          let result = await mongodb.findOne('user',where)
              console.log(result)
        if(result){
          	result.password = ''  // 清理掉密码，否则token太长了，影响到传输
          	let token = sign(result)
        	result = token
        }else if(result == null){
          response.error(ctx,'登录失败用户名或密码错误')
          return
        }
        
          if(result != false){
            response.success(ctx,result)
          }else{
            response.error(ctx,'服务器错误')
          }
            
            
        }
		// AiV-mKZ4g end
			
			// X5WzuKKUL Start
		
         findOneGetUserName = async (ctx)=>{ // user-获取用户名称
            
        	  
        	let userName = ctx.query.userName
            
        	ctx.checkQuery('userName','用户名称 不能为空').notEmpty()
          let errors = await ctx.validationErrors()
          if(errors){
            response.error(ctx,errors[0].msg)
            return
          }
            
        	let where = {userName}
            
            
            
            
            
          let result = await mongodb.findOne('user',where)
              if(result){
          result = true // 不要把用户信息返回给其他人
        }
          if(result != false){
            response.success(ctx,result)
          }else{
            response.error(ctx,'服务器错误')
          }
            
            
        }
		// X5WzuKKUL end
			
			// YcMZL0cRT Start
		
         findOneGetImageCode = async (ctx)=>{ // user-获取验证码
             const {
           token,
           buffer
         } = await captcha();
		 console.log(this)
        ctx.session.imageCode = token;
        // ctx.set('Content-Type','image/png')
        // ctx.body = buffer
        
        ctx.status = 200;
        ctx.type = "jpg";
        console.log(Buffer.isBuffer(buffer));
        ctx.length = Buffer.byteLength(buffer);
        ctx.body = buffer;
        
        return
        	  
            
          let errors = await ctx.validationErrors()
          if(errors){
            response.error(ctx,errors[0].msg)
            return
          }
            
        	let where = {}
            
            
            
            
            
          let result = await mongodb.findOne('user',where)
              
          if(result != false){
            response.success(ctx,result)
          }else{
            response.error(ctx,'服务器错误')
          }
            
            
        }
		// YcMZL0cRT end
			
			// _39SoIaMU Start
		
         saveUser = async (ctx)=>{ // user-注册用户
            
        const getUserName = async()=>{ //查询用户唯一
            
            let where = {userName:ctx.request.body.userName}
        	  
            return  await mongodb.findOne('user', where)
        }
        
        	  
        	let name = ctx.request.body.name
        	let age = ctx.request.body.age
        	let score = ctx.request.body.score
        	let userName = ctx.request.body.userName
        	let password = ctx.request.body.password
        	let token = ctx.request.body.token
        	let size = ctx.request.body.size
        	let auth = ctx.request.body.auth
        	let gold = ctx.request.body.gold
        	let imageCode = ctx.request.body.imageCode
            
        	ctx.checkBody('userName','用户名称 不能为空').notEmpty()
        	ctx.checkBody('password','密码 不能为空').notEmpty()
        	if(!auth){
        		auth = []
        	}
        	ctx.checkBody('imageCode','图像验证码 不能为空').notEmpty()
          let errors = await ctx.validationErrors()
          if(errors){
            response.error(ctx,errors[0].msg)
            return
          }
            
        	let where = {name,age,score,password,token,size,gold,auth,userName}
            
            
            where.createTime = new Date()
            where.updateTime = new Date()
        if(userName  == 'admin' || userName  == 'user'){
          
        }else{
          response.error(ctx,'超级管理员已达上限，不支持继续注册')
          return
        }
        
        
        if(imageCode != ctx.session.imageCode){ // 后续再严谨一点添加验证码过期时间
          response.error(ctx,'验证码错误，请重新输入')
          return
        }
        
        let isUserName = await getUserName()
        if(isUserName){
          response.error(ctx,'用户名称重复,请重新命名')
          return
        }
            
            
            
          let result = await mongodb.save('user',where)
              
          if(result != false){
            response.success(ctx,result)
          }else{
            response.error(ctx,'服务器错误')
          }
            
            
        }
		// _39SoIaMU end
			
			
			
			
			

}
var userController = new gui
module.exports =  userController